package com.jcfk.eam.service.equ;

import com.jcfk.common.business.service.BaseService;
import com.jcfk.eam.dao.main.EquStatisticsPointCheckOrderDao;
import com.jcfk.eam.domain.dto.main.EChartData;
import com.jcfk.eam.domain.dto.main.EChartDataItem;
import com.jcfk.eam.domain.dto.main.EquStatisticsDTO;
import com.jcfk.eam.domain.dto.main.EquStatisticsPointCheckOrderDTO;
import com.jcfk.eam.domain.po.main.EamPointCheckOrder;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EquStatisticsPointCheckOrderService extends BaseService<EquStatisticsPointCheckOrderDao, EamPointCheckOrder> {

    /**
     * 设备巡检数据统计
     */
    public EquStatisticsPointCheckOrderDTO queryPointCheckOrderList(EquStatisticsDTO query) {
        EquStatisticsPointCheckOrderDTO dto = new EquStatisticsPointCheckOrderDTO();
        dto.setAnnualStatistics(this.queryAnnualStatistics(query));
        dto.setAectionStatistics(this.queryAectionStatistics(query));
        dto.setTeamStatistics(this.queryTeamStatistics(query));
        dto.setTeamInspection(this.queryTeamInspection(query));
        dto.setPersonnelInspection(this.queryPersonnelInspection(query));
        return dto;
    }

    /**
     * 设备巡检-年度统计(折线)
     */
    private EChartData queryAnnualStatistics(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        List<EquStatisticsDTO> list = this.baseMapper.queryAnnualStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }

        list.forEach(item ->{
            eChartData.getXData().add(item.getXData());
            eChartData.getYData().add(item.getYData());
        });
        return eChartData;
    }

    /**
     * 设备巡检-工段统计(柱状)
     */
    @ApiModelProperty("设备巡检--工段统计(柱状)")
    private EChartData queryAectionStatistics(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        List<EquStatisticsDTO> list = this.baseMapper.queryAectionStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }

        list.forEach(item ->{
            eChartData.getXData().add(item.getXData());
            eChartData.getYData().add(item.getYData());
        });
        return eChartData;
    }

    /**
     * 设备巡检-班组统计(饼图)
     */
    @ApiModelProperty("设备巡检--班组统计(饼图)")
    private EChartData queryTeamStatistics(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        List<EChartDataItem> list = this.baseMapper.queryTeamStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }
        eChartData.setData(list);
        return eChartData;
    }

    /**
     * 人员巡检次数统计(柱状)
     */
    @ApiModelProperty("人员巡检次数统计(柱状)")
    private List<EChartData> queryPersonnelInspection(EquStatisticsDTO query) {
        List<EChartData> listData = new ArrayList<>();
        List<EquStatisticsDTO> list = this.baseMapper.queryPersonnelInspection(query);
        if(list == null || list.isEmpty()){
            return listData;
        }

        Map<String, List<EquStatisticsDTO>> groupedItems = list.stream()
                .collect(Collectors.groupingBy(EquStatisticsDTO::getXName));

        groupedItems.forEach((key,value)->{
            EChartData eChartData = new EChartData();
            eChartData.setName(key);
            value.forEach(item ->{
                eChartData.getXData().add(item.getXData());
                eChartData.getYData().add(item.getYData());
            });
            listData.add(eChartData);
        });

        return listData;
    }

    /**
     * 班组巡检结果-年度统计(柱状)
     */
    @ApiModelProperty("班组巡检结果-年度统计(柱状)")
    private List<EChartData> queryTeamInspection(EquStatisticsDTO query) {
        List<EChartData> listData = new ArrayList<>();
        List<EquStatisticsDTO> list = this.baseMapper.queryTeamInspection(query);
        if(list == null || list.isEmpty()){
            return listData;
        }
        Map<String, List<EquStatisticsDTO>> groupedItems = list.stream()
                .collect(Collectors.groupingBy(EquStatisticsDTO::getXListName));
        groupedItems.forEach((key,value)->{
            EChartData eChartData = new EChartData();
            eChartData.setName(key);
            Map<String, List<EquStatisticsDTO>> items = value.stream()
                    .collect(Collectors.groupingBy(EquStatisticsDTO::getXName));
            List<EChartData> listDataItem = new ArrayList<>();
            items.forEach((keyItme,valueItem)->{
                EChartData eChartDataItem = new EChartData();
                eChartDataItem.setName(keyItme);
                valueItem.forEach(item ->{
                    eChartDataItem.getXData().add(item.getXData());
                    eChartDataItem.getYData().add(item.getYData());
                });
                listDataItem.add(eChartDataItem);
            });
            eChartData.setListDate(listDataItem);
            listData.add(eChartData);
        });

        return listData;
    }
}
